All files / web/src/app/api/curriculum/[playerId]/advance route.ts

0% Statements 0/45
0% Branches 0/1
0% Functions 0/1
0% Lines 0/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                                           
/**
 * API route for advancing curriculum phase
 *
 * POST /api/curriculum/[playerId]/advance - Advance to next phase
 */

import { NextResponse } from 'next/server'
import { canPerformAction } from '@/lib/classroom'
import { advanceToNextPhase } from '@/lib/curriculum/progress-manager'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * POST - Advance player to next curriculum phase
 */
export const POST = withAuth(async (request, { params }) => {
  try {
    const { playerId } = (await params) as { playerId: string }

    if (!playerId) {
      return NextResponse.json({ error: 'Player ID required' }, { status: 400 })
    }

    // Authorization check
    const userId = await getUserId()
    const canView = await canPerformAction(userId, playerId, 'view')
    if (!canView) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const body = await request.json()
    const { nextPhaseId, nextLevel } = body

    if (!nextPhaseId) {
      return NextResponse.json({ error: 'Next phase ID required' }, { status: 400 })
    }

    const updated = await advanceToNextPhase(playerId, nextPhaseId, nextLevel)

    return NextResponse.json(updated)
  } catch (error) {
    console.error('Error advancing phase:', error)
    return NextResponse.json({ error: 'Failed to advance phase' }, { status: 500 })
  }
})